import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:jujij_shop/utils/dio_util.dart';
import '../widget/my_loading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../widget/my_dialog.dart';
import 'my/my_order.dart';
import 'my/my_service.dart';
import 'my/top_login.dart';
import '../widget/error_page.dart';
import '../widget/empty_page.dart';
class MyPage extends StatefulWidget {
  MyPage({Key key}) : super(key: key);
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
  bool isLogin = false; //是否登录
  Map<String, dynamic> userInfo; //用户信息
  Map<String, dynamic> menuData;//用户菜单数据

  //初始化用户信息
  @override
  void initState() {
    super.initState();
  }
  //初始化用户中心状态
  Future<List<Map<String,dynamic>>> initMyState() async {
    return  await Future.wait([
     getUserinfo(),
     getUserMenu()
    ]);
  }
  //获取用户菜单
  Future<Map<String,dynamic>> getUserMenu() async{
    String api='/api/mall/public/user/menu.service';
    DioUtil dio_util=DioUtil();
    print(await dio_util.get(api));
    return await dio_util.get(api);
  }

  //获取用户信息
  Future<Map<String,dynamic>> getUserinfo() async {
    DioUtil dioUtil = DioUtil();
    String api='/api/mall/user/userinfo.service';
    return await dioUtil.get(api);
  }



  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red ,
        title: Text('个人中心',
          style: TextStyle(
              color: Colors.white,
              fontSize: 30.sp,
              fontWeight: FontWeight.bold),),
        centerTitle: true,
      ),
      body:FutureBuilder<List<Map<String,dynamic>>>(
        future:  initMyState(),
        builder: (context, AsyncSnapshot snapshot) {

          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
              return CustomLoaddingDialog(
                  height: 150.0, width: 150, title: '努力加载中');
              break;
            case ConnectionState.done: // -------- 完成态
              if (snapshot.hasError) { // 异常
                return ErrorPage(error:snapshot.error.toString());
              }else{// 正常
                var userinfo=snapshot.data[0];
                var menuData=snapshot.data[1];
                if (userinfo['code'] == 200) {
                  var userinfoResult = userinfo['result'];
                  if(userinfoResult.isEmpty){
                    return EmptyPage();
                  }
                  return RefreshIndicator(
                      displacement: 20,
                      strokeWidth: 4,
                      color: Colors.red,
                      onRefresh: initMyState,
                      child: CustomScrollView(
                        physics: BouncingScrollPhysics(),
                        slivers: <Widget>[
                          SliverToBoxAdapter(
                            child:TopLogin(userInfo: userinfoResult) ,
                          ),
                          SliverToBoxAdapter(
                            child: MyOrder() ,
                          ),
                          SliverToBoxAdapter(
                            child: Container(
                              alignment: Alignment.center,
                              width: 750.w,
                              height: 120.w,
                              decoration: BoxDecoration(
                                  color: Colors.grey[200],
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Color.fromRGBO(244, 233, 244, 1), width: 1))),
                              child: Container(
                                width:720.w,
                                decoration: BoxDecoration(
                                    color:Colors.white,
                                    borderRadius: BorderRadius.circular(8.w)
                                ),
                                child: ListTile(
                                  title: Text('我的服务'),
                                ),
                              ),
                            ),
                          ),
                          SliverToBoxAdapter(
                            child: MyService(menuData:menuData),
                          ),
                          SliverToBoxAdapter(
                            child: Container(
                              alignment: Alignment.center,
                              color: Colors.grey[200],
                              height:150.w,
                              child: Text('聚集集提供技术支持'),
                            ),
                          )
                        ],
                      ));
                }else if(snapshot.data[0]['code']!=200){
                  return ErrorPage(error:snapshot.data[0]['msg']);
                }else if(snapshot.data[1]['code']!=200){
                  return ErrorPage(error:snapshot.data[1]['msg']);
                }
              }
              break;
            default:
              break;
          }
        },
      )



      ,
    );

  }

  @override
  bool get wantKeepAlive => true;
}
